<script lang="ts" setup>
import { useVbenModal } from '@vben/common-ui';

import { useVbenForm } from '#/adapter/form';

import { useSchema } from '../data';

const emit = defineEmits<{
  complete: [id: number, data?: number, remark?: string];
}>();

const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
  },
  schema: useSchema(),
  showDefaultActions: false,
});

const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    if (await formApi.validateAndSubmitForm()) {
      modalApi.close();
      const { id, value, remark } = await formApi.getValues();
      emit('complete', id, value, remark);
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (isOpen) {
      const { id, subjectName, value, remark } =
        modalApi.getData<Record<string, any>>();
      formApi.setValues({ id, subjectName, value, remark }, false);
    }
  },
  title: '填写点检信息',
  closeOnClickModal: false,
});
</script>

<template>
  <Modal>
    <Form />
  </Modal>
</template>
